# coding=utf-8
# your_accesskey_id：LTAI5tBUKa55Xc77mq2xChyv
# your_accesskey_secret：Tvnxg8BOpe1Y8edPACf4Q7PpH93gmS
# your_default_project：wenjun_monitor
# your_end_point：http://service.cn-chengdu.maxcompute.aliyun.com/api

# 阿里云计算操作对象
from odps import ODPS
import sys
import urllib
odps = ODPS('LTAI5tBUKa55Xc77mq2xChyv', 'Tvnxg8BOpe1Y8edPACf4Q7PpH93gmS', 'wenjun_monitor', endpoint='http://service.cn-chengdu.maxcompute.aliyun.com/api')

# ---------- 拿到需要写入数据的表实例 ----------
# table = odps.get_table('wenjun_monitor_release')
# print(table)

# ----------  开始写入数据 ----------
# data = [
#     ['appid456','pageid456','20220515','ua456','https://www.baidu.com','{"a":"456"}','pv']
# ]
# odps.write_table(table, data, partition='datetime=20220513')

# ---------- 读取数据库列表 ----------
# for table in odps.list_tables():
#     print(table)

# ---------- 使用SQL语句的方式插入数据 ----------
# INSERT INTO wenjun_monitor.wenjun_monitor
# PARTITION (datetime = '20220513')
# VALUES (1,'2022-05-13 09:33','{"a":"1"}'),
# (1,'2022-05-13 10:33','{"a":"1"}'),
# (1,'2022-05-13 11:33','{"a":"1"}');
# new_table = o.execute_sql('select * from wenjun_monitor.wenjun_monitor',hints = {'odps.sql.allow.fullscan':'true'})
# insert_sql = 'INSERT INTO wenjun_monitor.wenjun_monitor_release PARTITION (datetime = "20220513") VALUES ("appid123","pageid123","20220514","ua123","https://www.baidu.com",\'{"a":"123"}\',"pv")'
# odps.execute_sql(insert_sql)

# ---------- sys 获取命令行参数----------
# print(sys.argv)

# ---------- 执行脚本获取的SQL语句 ----------
insertSql = urllib.unquote(sys.argv[1])
# 打印格式化 url decode
print(insertSql)

odps.execute_sql(insertSql)
